package controller.behavior;

import model.BulletModel;
import model.Model;
import model.TankModel;
import util.Direction;
import view.ModelView;
import view.TankView;

public class EasyAI implements Behavior {
    private int moveCount; // 移动的步数
    private int fireCount; // 开火的步数
    private TankModel tankModel;
    private TankView tankView;

    public EasyAI(TankModel model, TankView tankView) {
        moveCount = 10;
        fireCount = 5;
        tankModel = model;
        this.tankView = tankView;
    }

    private boolean checkFire() {
        int fireNum = (int) (Math.random() * 10);
        if (fireNum <= 2 && fireCount-- == 0) {
            fireCount = (int) (Math.random() * (10 - 5) + 5);
            return true;
        }
        return false;
    }

    private boolean checkDir() {
        if (moveCount-- != 0)
            return false;
        moveCount = (int) (Math.random() * (10 - 5) * 5);
        return true;
    }

    private Direction getNewDir() {
        int dirNum = (int) (Math.random() * Direction.values().length - 1);
        return Direction.values()[dirNum];
    }

    @Override
    public BulletModel fire(boolean isfire) {
        if (isfire && checkFire()) {
            BulletModel bulletModel = new BulletModel(tankModel.getCamp(), Integer.valueOf(tankModel.getBullet()));
            return bulletModel;
        } else
            return null;
    }

    // 如果需要改变方向则回调下一个的方向，如果不需要则回调当前方向
    @Override
    public Direction move(boolean ismove) {
        if (ismove) {
            if (checkDir())
                return getNewDir();
            else
                return tankView.getDir();
        } else
            return null;
    }

    @Override
    public void stay() {
        tankView.stay();
    }

    @Override
    public ModelView getView() {
        return tankView;
    }

    @Override
    public Model getModel() {
        return tankModel;
    }

    @Override
    public boolean isLife() {
        if (tankView.getBloodView().getMode() <= 0)
            return false;
        else
            return true;
    }
}
